<?php
namespace Home\Controller;
use Think\Controller;

class LoginController extends CommonController {
    	public function index(){
    		$res = D('verify')->getOneverify();
	   		$this->assign('res', $res);
	   		$this->display();
    	}
   
    	
    	public function checkdate(){
    		//人类验证
    		// var_dump(I('post.'));
    		$verify = I('post.verify');
    		if ($verify!=$_SESSION['answer']) {
    			$arr = array(
    					'error' => 1,
    					'info' => '人类验证错误'
    				);
    			echo json_encode($arr);
    			exit();
    		}
    		//用户名密码验证
    		$user = I('post.email');
    		// var_dump(D('user')->checkEmail($user));exit();
    		$password = I('post.password');
  		if ($res= D('user')->checkdate($user,$password)) {
  			$arr = array(
    					'error' => 0,
    					'info' => '登录成功'
    				);

    			echo json_encode($arr);
    			$_SESSION['id']=$res['id'];
    			$_SESSION['nickname']=$res['nickname'];
    			$_SESSION['face']=$res['face'];
  		}else{
  			$arr = array(
    					'error' => 1,
    					'info' => '用户名密码错误'
    				);
    			echo json_encode($arr);
    			exit();
  		}
    	
    
	}

	public function loginout(){
		session_unset();
		session_destroy();
		$this->success('退出成功',U('home/index/index'),0);
	}



            //qq互联回调的方法
            public function qqreturn()
            {
                header("Content-type: text/html; charset=utf-8");

                $openid = getOpenid();

                echo $openid;

                if ($res = M('user')->where(array('openid'=>$openid))->find()) {
                    //如果有账号成功绑定了的
                    $_SESSION['id']=$res['id'];
                    $_SESSION['nickname']=$res['nickname'];
                    $_SESSION['face']=$res['face'];
                    $this->success('成功登录',U('home/index/index'),3);
                }else{
                    //没有账号的
                    //得到用户信息
                    $userinfo = getUserInfo();

                    var_dump($userinfo);

                    $data['nickname'] = $userinfo['nickname'];

                    $data['create_time'] = $userinfo['city'];

                    $data['openid'] = $openid;

                    if ($res = M('user')->add($data)) {
                        $_SESSION['id'] = $res;
                        $_SESSION['nickname']=$data['nickname'];
                        $this->success('成功登录',U('home/index/index'),3);
                    }
                   
                }
                



            }

            function qqlogin(){
                helogin();
            }



}









